
package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableRowSorter;

import main.Conference;
import main.Manuscript;

/**
 *
 * @author Holly Beach
 * @version 12/3/11
 *
 */
public class PCViewsFrame {
	
	//GUI Stuff
	private JFrame my_mainGUI;
	private JPanel my_close_panel;
	private JTable my_gui_table;
	
	//Data Stuff
	private Conference my_conference;
	
	public PCViewsFrame(Conference conf){
		my_mainGUI = new JFrame("Manuview -- View all Manuscripts");
		my_close_panel = new JPanel();
		my_conference = conf;		
	}
	
	public void start() {
		Object[][] tableData = new Object[my_conference.getManuscripts().size()][5];
		setTableData(tableData);
		String[] colNames = {"Title","Author","# of Reviews", "Status", " "};
		ManuTableModel dataModel= new ManuTableModel(tableData, colNames);
		my_gui_table = new JTable(dataModel);
		my_gui_table.addMouseListener(new TableMouseListener(my_gui_table));
		my_gui_table.setRowSorter(new TableRowSorter(dataModel));
		my_gui_table.setFillsViewportHeight(true);
		my_gui_table.setShowGrid(true);
		my_gui_table.setGridColor(Color.DARK_GRAY);
		my_gui_table.setRowHeight(50);
		my_gui_table.getTableHeader().setPreferredSize(
				new Dimension(my_gui_table.getTableHeader().getWidth(),50)); 
		my_gui_table.setCellSelectionEnabled(false);
		my_gui_table.setSelectionBackground(my_gui_table.getBackground());
		my_gui_table.setSelectionForeground(my_gui_table.getForeground());
		//set renderer for change status button in the JTable
		TableColumn col = my_gui_table.getColumnModel().getColumn(4);
		col.setCellRenderer(new TableButton());
		//set renderer for center alignment of column 2
		col = my_gui_table.getColumnModel().getColumn(2);
		col.setCellRenderer(new CenterRenderer());
		//center allign all the column headings
		TableCellRenderer renderer = my_gui_table.getTableHeader().getDefaultRenderer();
		JLabel label = (JLabel)renderer;
		label.setHorizontalAlignment(JLabel.LEFT);


		Border blackborder = BorderFactory.createLineBorder(Color.BLACK);
		JScrollPane sp = new JScrollPane(my_gui_table);
		sp.setBorder(blackborder);
		my_mainGUI.add(sp, BorderLayout.CENTER);
		JButton closeButton = new JButton("CLOSE");
		closeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				my_conference.save();
				StartFrame back = new StartFrame();
				back.start();
				my_mainGUI.dispose();
			}
		});
		my_close_panel.add(closeButton);
		my_close_panel.setBorder(blackborder);
		my_mainGUI.add(my_close_panel,BorderLayout.SOUTH);
		my_mainGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		my_mainGUI.setSize(700, 350);
		my_mainGUI.setVisible(true);
	}
	
	/**
	 * This method populates a 2D array with manuscript data and 
	 * Change Status buttons for displaying in a JTable 
	 */
	private void setTableData(Object[][] dataArray) {
		for (int i=0; i< my_conference.getManuscripts().size(); i++){
			Manuscript man = my_conference.getManuscripts().get(i);
				dataArray[i][0] = man.getTitle();
				dataArray[i][1] = man.getAuthor();
				dataArray[i][2] = man.getNumReviews();
				dataArray[i][3] = man.getStatus();
				TableButton chgStatusButton = new TableButton();
				chgStatusButton.addActionListener(new SetStatusButtonListener(man));
				dataArray[i][4] = chgStatusButton;
			}
	}
	
	//used for testing
	/*
	public static void main(String args[]){
		Conference conf = new Conference();
		conf.addManuscript(new Manuscript("Manuscript 1", "Joe", "abc", "text"));
		conf.addManuscript(new Manuscript("Manuscript 2", "Abe", "abc", "text"));
		conf.addManuscript(new Manuscript("Manuscript 3", "Becca", "abc", "text"));
		PCViewsFrame gui = new PCViewsFrame(conf);
		gui.start();
	}
	*/
	
	/////////////////////////////////////////////////////////////
	//////////////*Inner Classes for JTable*/////////////////////
	/////////////////////////////////////////////////////////////
	
	/**
	 * @author Holly- this class sets the specifications for the JTable model
	 */
	@SuppressWarnings("serial")
	class ManuTableModel extends DefaultTableModel {
		private Object data[][];
		
		public ManuTableModel(Object[][] dataArr, String[] colNames){
			super(dataArr, colNames);
			data=dataArr;
		}
		//this method is used to get the button object out of the JTable for event handling
		public Object getValueAt(int row, int col) {
	        return data[row][col];
	    }
		
		@Override
		 public Class getColumnClass(int c) {
		        return getValueAt(0, c).getClass();
		    }

		@Override
		public boolean isCellEditable(int row, int col){
			return false;
		}
	}//end of ManuTableModel class
	
	/**
	 * @Author Holly - this class is necessary to center align the # of reviews column
	 */
	@SuppressWarnings("serial")
	private class CenterRenderer extends DefaultTableCellRenderer{

		@Override
		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {
			super.getTableCellRendererComponent(table, value, isSelected, 
					hasFocus, row, column);
			
			setHorizontalAlignment(SwingConstants.CENTER);
			return this;
		}		
	}//end of CenterRenderer class
	
	/**
	 * @author Holly -This class is necessary to render the "Change Status" button 
	 * for display within the table.
	 */
	@SuppressWarnings("serial")
	private class TableButton extends JButton implements TableCellRenderer{
		public TableButton(){
			super("Change Status");
		}
		
		@Override
		public Component getTableCellRendererComponent(JTable table,Object value, 
													boolean isSelected, boolean hasFocus, 
													int row,int column) {
			
			this.setText( ((TableButton)value).getText() );
			this.addActionListener(((TableButton)value).getActionListeners()[0]);
			return this;	
		}
	} //end of TableButton class
	
	/**
	 * @author Holly -This MouseListener class is necessary for recognizing clicks on the
	 * "Change Status" button and activating its assigned actionlistener 
	 */
	private class TableMouseListener implements MouseListener{
		JTable table;
		
		public TableMouseListener(JTable aTable){
			table=aTable;
		}

		@Override
		public void mouseClicked(MouseEvent e) {
			//see if the mouse was over a button when this happened,
			//if so, get the value of that cell and call doClick
			int row = table.rowAtPoint(e.getPoint());
		    int col = table.columnAtPoint(e.getPoint());
		    if (col == 4) {
		    	TableButton button = (TableButton) table.getValueAt(row, col);
		    	button.doClick();
		    }	    
		}

		@Override //do nothing
		public void mousePressed(MouseEvent e) { }

		@Override //do nothing
		public void mouseReleased(MouseEvent e) { }

		@Override //do nothing
		public void mouseEntered(MouseEvent e) { }

		@Override //do nothing
		public void mouseExited(MouseEvent e) {	}	
	}//end of TableMouseListener class
	
	/**
	 * @author Holly -This class holds a manuscript object for passing to the 
	 * changestatus screen 
	 */
	private class SetStatusButtonListener implements ActionListener{
		Manuscript my_manu;
		
		public SetStatusButtonListener(Manuscript aManuscript){
			my_manu = aManuscript;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println("Change status on " + my_manu.getTitle());
			//TODO after the fields and constructor are ready change to 
			PCDecisionFrame df = new PCDecisionFrame(my_manu, my_conference);
			//PCDecisionFrame df = new PCDecisionFrame();
			df.start();
			my_mainGUI.dispose();		
		}
	}//end of SetStatusButtonListener class
	
}//end of PCViewsFrame
